671f17a9e41072b807d914e721b648bd34200d29
[project/luci.git] /
1 'use strict';
2 'require view';
3 'require poll';
4 'require form';
5 'require uci';
6 'require fs';
7 'require network';
8 'require rpc';
9 'require shadowsocks-libev as ss';
10
11 var conf = 'shadowsocks-libev';
12 var cfgtypes = ['ss_local', 'ss_redir', 'ss_server', 'ss_tunnel'];
13
14 var callServiceList = rpc.declare({
15 object: 'service',
16 method: 'list',
17 params: [ 'name' ],
18 expect: { '': {} }
19 });
20
21 return view.extend({
22 render: function(stats) {
23 var m, s, o;
24
25 m = new form.Map(conf,
26 _('Local Instances'),
27 _('Instances of shadowsocks-libev components, e.g. ss-local, \
28 ss-redir, ss-tunnel, ss-server, etc. To enable an instance it \
29 is required to enable both the instance itself and the remote \
30 server it refers to.'));
31
32 s = m.section(form.GridSection);
33 s.addremove = true;
34 s.cfgsections = function() {
35 return this.map.data.sections(this.map.config)
36 .filter(function(s) { return cfgtypes.indexOf(s['.type']) !== -1; })
37 .map(function(s) { return s['.name']; });
38 };
39 s.sectiontitle = function(section_id) {
40 var s = uci.get(conf, section_id);
41 return (s ? s['.type'] + '.' : '') + section_id;
42 };
43 s.renderSectionAdd = function(extra_class) {
44 var el = form.GridSection.prototype.renderSectionAdd.apply(this, arguments),
45 optionEl = [E('option', { value: '_dummy' }, [_('-- instance type --')])];
46 cfgtypes.forEach(function(t) {
47 optionEl.push(E('option', { value: t }, [t.replace('_', '-')]));
48 });
49 var selectEl = E('select', {
50 class: 'cbi-input-select',
51 change: function(ev) {
52 ev.target.parentElement.nextElementSibling.nextElementSibling
53 .toggleAttribute('disabled', ev.target.value === '_dummy');
54 }
55 }, optionEl);
56 el.lastElementChild.setAttribute('disabled', '');
57 el.prepend(E('div', {}, selectEl));
58 return el;
59 };
60 s.handleAdd = function(ev, name) {
61 var selectEl = ev.target.parentElement.firstElementChild.firstElementChild,
62 type = selectEl.value;
63 this.sectiontype = type;
64 var promise = form.GridSection.prototype.handleAdd.apply(this, arguments);
65 this.sectiontype = undefined;
66 return promise;
67 };
68 s.addModalOptions = function(s, section_id, ev) {
69 var sdata = uci.get(conf, section_id),
70 stype = sdata ? sdata['.type'] : null;
71 if (stype) {
72 s.sectiontype = stype;
73 return Promise.all([
74 L.resolveDefault(fs.stat('/usr/bin/' + stype.replace('_', '-')), null),
75 network.getDevices()
76 ]).then(L.bind(function(res) {
77 s.tab('general', _('General Settings'));
78 s.tab('advanced', _('Advanced Settings'));
79 s.taboption('general', form.Flag, 'disabled', _('Disable'));
80 if (!res[0]) {
81 ss.option_install_package(s, 'general');
82 }
83 ss.options_common(s, 'advanced');
84
85 if (stype === 'ss_server') {
86 ss.options_server(s, { tab: 'general' });
87 o = s.taboption('advanced', form.Value, 'local_address',
88 _('Local address'),
89 _('The address ss-server will initiate connections from'));
90 o.datatype = 'ipaddr';
91 ss.values_ipaddr(o, res[1]);
92 o = s.taboption('advanced', form.Value, 'local_ipv4_address',
93 _('Local IPv4 address'),
94 _('The IPv4 address ss-server will initiate IPv4 connections from'));
95 o.datatype = 'ip4addr';
96 ss.values_ip4addr(o, res[1]);
97 o = s.taboption('advanced', form.Value, 'local_ipv6_address',
98 _('Local IPv6 address'),
99 _('The IPv6 address ss-server will initiate IPv6 connections from'));
100 o.datatype = 'ip6addr';
101 ss.values_ip6addr(o, res[1]);
102 } else {
103 ss.options_client(s, 'general', res[1]);
104 if (stype === 'ss_tunnel') {
105 o = s.taboption('general', form.Value, 'tunnel_address',
106 _('Tunnel address'),
107 _('The address ss-tunnel will forward traffic to'));
108 o.datatype = 'hostport';
109 }
110 }
111 }, this));
112 }
113 };
114
115 o = s.option(form.DummyValue, 'overview', _('Overview'));
116 o.modalonly = false;
117 o.editable = true;
118 o.rawhtml = true;
119 o.renderWidget = function(section_id, option_index, cfgvalue) {
120 var sdata = uci.get(conf, section_id);
121 if (sdata) {
122 return form.DummyValue.prototype.renderWidget.call(this, section_id, option_index, ss.cfgvalue_overview(sdata));
123 }
124 return null;
125 };
126
127 o = s.option(form.DummyValue, 'running', _('Running'));
128 o.modalonly = false;
129 o.editable = true;
130 o.default = '';
131
132 o = s.option(form.Button, 'disabled', _('Enable/Disable'));
133 o.modalonly = false;
134 o.editable = true;
135 o.inputtitle = function(section_id) {
136 var s = uci.get(conf, section_id);
137 if (ss.ucival_to_bool(s['disabled'])) {
138 this.inputstyle = 'reset';
139 return _('Disabled');
140 }
141 this.inputstyle = 'save';
142 return _('Enabled');
143 }
144 o.onclick = function(ev) {
145 var inputEl = ev.target.parentElement.nextElementSibling;
146 inputEl.value = ss.ucival_to_bool(inputEl.value) ? '0' : '1';
147 return this.map.save();
148 }
149
150 return m.render().finally(function() {
151 poll.add(function() {
152 return L.resolveDefault(callServiceList(conf), {})
153 .then(function(res) {
154 var instances = null;
155 try {
156 instances = res[conf]['instances'];
157 } catch (e) {}
158 if (!instances) return;
159 uci.sections(conf)
160 .filter(function(s) { return cfgtypes.indexOf(s['.type']) !== -1; })
161 .forEach(function(s) {
162 var el = document.getElementById('cbi-shadowsocks-libev-' + s['.name'] + '-running');
163 if (el) {
164 var name = s['.type'] + '.' + s['.name'],
165 running = instances.hasOwnProperty(name)? instances[name].running : false;
166 el.innerText = running ? 'yes' : 'no';
167 }
168 });
169 });
170 });
171 });
172 },
173 });